<?php
header('Content-Type: text/html; charset=UTF-8');
require 'crawl.php';
require 'database.php';
$db = new DB();

// Select games ID
$Qry = 'SELECT id, crawl_url
		FROM games
		WHERE done = 0 AND crawl_url != ""
		ORDER BY id DESC
		LIMIT 30';
$res = $db->fetchAll($Qry);

foreach ($res as $key=>$value) {
	crawl_2($value['id'], $value['crawl_url']);
}

function crawl_2($id, $url) {
	global $db;

	$link = 'http://www.oyungemisi.com' . $url;

	$search = array('"', ',', ' ');

	$html = file_get_contents($link);
// 	echo $html;

	$game_file = preg_replace('/\<!doctype html\>(.*?)location    :/si', '', $html);
	$game_file = preg_replace('/format      :(.*?)\<\/html\>/si', '', $game_file);

	$game_file = str_replace($search, '', $game_file);
	$game_file = trim($game_file);
	$game_file = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $game_file);

// 	echo $game_file . '<br />';

	if ($game_file) {
		$query = 'UPDATE games SET url = "' . $game_file . '", done = 1 WHERE id = ' . $id;
		$db->Query($query);

		echo '<div>' . $query . '</div>';
	}
}

function crawl($id, $url) {
	$link = 'http://www.oyuncuyuz.biz.tr/oyna/' . $url . '.html';
// 	$link = 'http://www.oyuncuyuz.biz.tr/oyna/fishao.html';

	$html = file_get_html($link);

	foreach ($html->find('div#reklam') as $e)
		$e->outertext = '';

	if ($html->find('div#oyun', 0))
		$string = $html->find('div#oyun', 0)->innertext;
	else
		$string = '';

	if (!$string) {
		return false;
		$html->clear();
		unset($html);
	}

	$str = str_get_html($string);
// 	echo $str;
// 	exit();

	$game_file = '';
	// SWF
	$pos = strpos($str, '.swf');
	if ($pos !== false) {
// 		echo $id . ' swf ok<br />';

		$arr = array('swf("', '<!--', '<script type="text/rocketscript">', '<script type="text/rocketscript" data-rocketoptimized="true">');
		$str = str_replace($arr, '', $str);
		$str = trim($str);
// 		echo $str;
// 		exit();
		$game_file = preg_replace('/\.swf(.*?)\<\/script\>/si', '.swf', $str);
// 		echo $game_file;
	}

	// UNITY
	$pos = strpos($str, '.unity3d');
	if ($pos !== false) {
// 		echo $id . ' unity3d ok<br />';
		$str = strip_tags($str, 'script');
		$str = str_replace('unityObject.embedUnity("unityPlayer",', '', $str);
		$str = str_replace('"', '', $str);
		$str = trim($str);
// 		echo $str;

		$game_file = preg_replace('/\.unity3d(.*?)\);/si', '.unity3d', $str);
// 		echo $game_file;
	}

	// DCR
	$pos = strpos($str, '.dcr');
	if ($pos !== false) {
		$acb = str_get_html($str);

		foreach ($acb->find('embed') as $element){
			$game_file = trim($element->src);
		}
	}

	// IFRAME
	$pos = strpos($str, 'iframe');
	if ($pos !== false) {
// 		echo $id . ' iframe ok<br />';

		$acb = str_get_html($str);

		foreach ($acb->find('iframe') as $element){
			$game_file = trim($element->src);
		}
	}
// 	echo $game_file;

	$query = '';
	if ($game_file)
		$query = 'UPDATE games SET url = "' . $game_file . '" WHERE id = ' . $id;

	return $query;
}